package io.renren.modules.evaluate.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.evaluate.dao.EvaluationSchemeMapper;
import io.renren.modules.evaluate.entity.EvaluationSchemeEntity;
import io.renren.modules.evaluate.entity.IndexSystemMidEntity;
import io.renren.modules.evaluate.entity.SchemeIndexEntity;
import io.renren.modules.evaluate.service.EvaluationSchemeService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("generator/evaluationscheme")
public class EvaluationSchemeController {

    @Autowired
    EvaluationSchemeService evaluationSchemeService;
    @Autowired
    EvaluationSchemeMapper evaluationSchemeMapper;
    @Autowired
    private HttpServletRequest request;

    @ApiOperation("评估方案的展示")
    @GetMapping("/evaluation/findById")
    public List<EvaluationSchemeEntity> findById(Integer id){
        return evaluationSchemeService.findById(id);
    }

    @ApiOperation("展示所有评估方案")
    @GetMapping("/evaluation/findall")
    public List<EvaluationSchemeEntity> findAll(){ return evaluationSchemeService.findAll();}











     /*** * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:evaluationscheme:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = evaluationSchemeService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 查看评估方案的名称是否重复
     */
    @ApiOperation("queryBySchemeName")
    @PostMapping("/isRepeated")
//    @RequiresPermissions("generator:evaluationscheme:info")
    public R isRepeated(@RequestBody String schemeName){
        List<EvaluationSchemeEntity> evals = evaluationSchemeService.queryBySchemeName(schemeName);
        if(evals.size() > 0){
            return R.error(-1,"方案名称重复");
        }
        return R.ok();
    }

    /**
     * 查看评估方案
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:evaluationscheme:info")
    public R info(@PathVariable("id") Integer id){
        EvaluationSchemeEntity evaluationScheme = evaluationSchemeService.getById(id);

        return R.ok().put("evaluationScheme", evaluationScheme);
    }

    /**
     * 根据方案名模糊查询
     */
    @ApiOperation("fuzzyQueryBySchemeName")
    @PostMapping("/fuzzyQuery")
//    @RequiresPermissions("generator:evaluationscheme:info")
    public R fuzzyQuery(@RequestBody String schemeName){
        HttpSession session = request.getSession();
        Long userId = (Long)session.getAttribute("userId");
        System.out.println(userId);
        List<EvaluationSchemeEntity> evals = evaluationSchemeService.fuzzyQueryBySchemeName(schemeName, userId);

        return R.ok().put("evaluationSchemeMes",evals);
    }


    /**
     * 根据某一个父指标查看其直接子指标
     * @Param
     * 1. 方案的id：从方案-指标表中查找， tb_scheme_index
     * 2. 指标体系的id，从tb_idexsys_index 中查找
     * 3. 父指标id
     */
    @ApiOperation("querySonIdxByFatherIdx")
    @PostMapping("/queryByFatherIndex")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "schemeId", dataType = "object", value="1"),
            @ApiImplicitParam(paramType = "query",name="indexSystemId", dataType = "object", value = "2"),
            @ApiImplicitParam(paramType = "query",name="pid", dataType = "object", value = "2")
    })
    public R queryByFatherIndex(@ApiIgnore @RequestParam(required = false) Map<String, Object> params){
        Object schemeId = params.get("schemeId");
        Object indexSysId = params.get("indexSystemId");
        Object parentId = params.get("pid");
        Integer idxSysId = Integer.parseInt(indexSysId.toString());
        Integer pid = Integer.parseInt(parentId.toString());
        if(schemeId != null){
            Integer schId = Integer.parseInt(schemeId.toString());
            List<SchemeIndexEntity> schemeIndexEntityList = evaluationSchemeService.queryByParentIdx(schId, pid);
            System.out.println(schemeIndexEntityList);
            return R.ok().put("children", schemeIndexEntityList);
        }else if(schemeId == null){
            List<IndexSystemMidEntity> indexSystemMidEntityList = evaluationSchemeService.queryByParentIdx2(idxSysId, pid);
            System.out.println(indexSystemMidEntityList);
            return R.ok().put("children2", indexSystemMidEntityList);
        }
        return R.error(-1, "传递的信息为空");
    }


    /**
     * 根据方案id，查看方案的具体信息
     */
    @PostMapping("/queryBySchemeId")
    public R queryBySchemeId(@RequestBody Integer schemeId){
        // 1. 查找方案的具体信息
//        List<EvaluationSchemeEntity> evals = evaluationSchemeService.fuzzyQueryBySchemeName(schemeName, userId);
        return R.ok();
    }


    /**
     * 新增评估方案
     */
    @ApiOperation("addEvaluationScheme")
    @PostMapping("/add")
//    @RequiresPermissions("generator:evaluationscheme:save")
    public R add(@RequestBody EvaluationSchemeEntity evaluationScheme){
        // 1.保存评估方案
        evaluationScheme.setStatus("0");
        evaluationScheme.setEstablishedTime(new Date());
        Integer rows = evaluationSchemeService.addEvaluationScheme(evaluationScheme);
        // 2. 保存方案对应的指标信息
        Integer schemeId = evaluationScheme.getId();
        List<SchemeIndexEntity> schemeIndexEntityList = evaluationScheme.getSchemeMidEntityList();
        System.out.println(schemeIndexEntityList);
        Integer rowss = evaluationSchemeService.addSchemeIndex(schemeIndexEntityList, schemeId);

        return R.ok();
    }

    /**
     * 评估方案的修改
     */
    @ApiOperation("updateScheme")
    @PostMapping("/update")
//    @RequiresPermissions("generator:evaluationscheme:update")
    public R update(@RequestBody EvaluationSchemeEntity evaluationScheme){
        // 修改评估方案的信息
        evaluationSchemeService.updateById(evaluationScheme);
        // 修改评估方案中，指标的权重，或权重算法
        List<SchemeIndexEntity> schemeIndexEntityList = evaluationScheme.getSchemeMidEntityList();
        System.out.println(schemeIndexEntityList.get(0).getWeight());
        System.out.println(schemeIndexEntityList.get(0).getWeightMethodId());
        evaluationSchemeService.updateSchemeIndexById(schemeIndexEntityList);
        return R.ok();

    }

    /**
     * 逻辑删除评估方案
     */
    @ApiOperation("deleteEvaluationScheme")
    @DeleteMapping("/delete")
//    @RequiresPermissions("generator:evaluationscheme:delete")
    public R delete(@RequestBody Integer id){
        evaluationSchemeService.logicalDelById(id);

        return R.ok();
    }

}
